package com.castle.fortress.admin.oa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.oa.entity.HolidayTypeEntity;
import com.castle.fortress.admin.oa.dto.HolidayTypeDto;
import com.castle.fortress.admin.oa.service.HolidayTypeService;
import com.castle.fortress.admin.oa.service.UserHolidaySettingService;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.enums.YesNoEnum;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;

import java.util.Arrays;
import java.util.List;

/**
 * 假期类型 控制器
 *
 * @author whc
 * @since 2022-11-03
 */
@Api(tags="假期类型管理控制器")
@Controller
public class HolidayTypeController {
    @Autowired
    private HolidayTypeService holidayTypeService;
    @Autowired
    private UserHolidaySettingService userHolidaySettingService;

    /**
     * 假期类型的分页展示
     * @param holidayTypeDto 假期类型实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation = "假期类型分页展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("假期类型分页展示")
    @GetMapping("/oa/holidayType/page")
    @ResponseBody
    @RequiresPermissions("oa:holidayType:pageList")
    public RespBody<IPage<HolidayTypeDto>> pageHolidayType(HolidayTypeDto holidayTypeDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<HolidayTypeDto> page = new Page(pageIndex, pageSize);
        IPage<HolidayTypeDto> pages = holidayTypeService.pageHolidayTypeExtends(page, holidayTypeDto);

        return RespBody.data(pages);
    }

    /**
     * 假期类型的列表展示
     * @param holidayTypeDto 假期类型实体类
     * @return
     */
    @CastleLog(operLocation = "假期类型分页展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("假期类型列表展示")
    @GetMapping("/oa/holidayType/list")
    @ResponseBody
    public RespBody<List<HolidayTypeDto>> listHolidayType(HolidayTypeDto holidayTypeDto){
        List<HolidayTypeDto> list = holidayTypeService.listHolidayType(holidayTypeDto);
        return RespBody.data(list);
    }

    /**
     * 假期类型保存
     * @param holidayTypeDto 假期类型实体类
     * @return
     */
    @CastleLog(operLocation = "假期类型保存", operType = OperationTypeEnum.INSERT)
    @ApiOperation("假期类型保存")
    @PostMapping("/oa/holidayType/save")
    @ResponseBody
    @RequiresPermissions("oa:holidayType:save")
    public RespBody<String> saveHolidayType(@RequestBody HolidayTypeDto holidayTypeDto){
        if(holidayTypeDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        HolidayTypeEntity holidayTypeEntity = ConvertUtil.transformObj(holidayTypeDto,HolidayTypeEntity.class);
        if(holidayTypeService.save(holidayTypeEntity)){
            userHolidaySettingService.asyncNewType(holidayTypeEntity);
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 假期类型编辑
     * @param holidayTypeDto 假期类型实体类
     * @return
     */
    @CastleLog(operLocation = "假期类型编辑", operType = OperationTypeEnum.UPDATE)
    @ApiOperation("假期类型编辑")
    @PostMapping("/oa/holidayType/edit")
    @ResponseBody
    @RequiresPermissions("oa:holidayType:edit")
    public RespBody<String> updateHolidayType(@RequestBody HolidayTypeDto holidayTypeDto){
        if(holidayTypeDto == null || holidayTypeDto.getId() == null || holidayTypeDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        HolidayTypeEntity holidayTypeEntity = ConvertUtil.transformObj(holidayTypeDto,HolidayTypeEntity.class);
        if (!YesNoEnum.NO.getCode().equals(holidayTypeEntity.getIsLimit())) {
            final HolidayTypeDto oldData = holidayTypeService.getByIdExtends(holidayTypeDto.getId());
            if (!oldData.getAmount().equals(holidayTypeEntity.getAmount())) {
                //如果数量更改了 那就异步修改所有用户的额度 为新的数量
                userHolidaySettingService.asyncUpdateUserSetting(holidayTypeEntity);
            }
        }
        if(holidayTypeService.updateById(holidayTypeEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 假期类型删除
     * @param id
     * @return
     */
    @CastleLog(operLocation = "假期类型删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("假期类型删除")
    @PostMapping("/oa/holidayType/delete")
    @ResponseBody
    @RequiresPermissions("oa:holidayType:delete")
    public RespBody<String> deleteHolidayType(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(holidayTypeService.removeById(id)) {
            userHolidaySettingService.asyncRemoveUserSetting(Arrays.asList(id));
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 假期类型批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation = "假期类型批量删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("假期类型批量删除")
    @PostMapping("/oa/holidayType/deleteBatch")
    @ResponseBody
    @RequiresPermissions("oa:holidayType:deleteBatch")
    public RespBody<String> deleteHolidayTypeBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(holidayTypeService.removeByIds(ids)) {
            userHolidaySettingService.asyncRemoveUserSetting(ids);
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 假期类型详情
     * @param id 假期类型id
     * @return
     */
    @CastleLog(operLocation = "假期类型详情", operType = OperationTypeEnum.QUERY)
    @ApiOperation("假期类型详情")
    @GetMapping("/oa/holidayType/info")
    @ResponseBody
    @RequiresPermissions("oa:holidayType:info")
    public RespBody<HolidayTypeDto> infoHolidayType(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        HolidayTypeDto holidayTypeDto =  holidayTypeService.getByIdExtends(id);

        return RespBody.data(holidayTypeDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation = "假期类型导出", operType = OperationTypeEnum.EXPORT)
	@PostMapping("/oa/holidayType/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<HolidayTypeDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<HolidayTypeDto> list = holidayTypeService.listHolidayType(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
